/**  
 * @Title:  HZQClassLoader.java   
 * @Package Dongtai   
 * @Description:    TODO   
 * @author: hezhiqin     
 * @date:   2019年4月2日 上午10:07:55   
 */  
package Dongtai;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**   
 * @ClassName:  HZQClassLoader   
 * @Description:TODO(自己创建的ClassLoader)   
 * @author: hezhiqin
 * @date:   2019年4月2日 上午10:07:55   
 *      
 */
public class HZQClassLoader extends ClassLoader {
	
	private File classPathFile;
	
	
	public HZQClassLoader() {
		
		String classPath = HZQClassLoader.class.getResource("").getPath();
		this.classPathFile = new File(classPath);
		
	}
	
	protected Class<?> findClass(String name) throws ClassNotFoundException{
		
		//获取这个类的地址
		String className = HZQClassLoader.class.getPackage().getName() + "." + name;
		
		if (classPathFile!=null) {
			//获取这个文件
			File classFile = new File(classPathFile,name.replaceAll("\\.","/") + ".class");
			if(classFile.exists()){
				FileInputStream in = null;
				ByteArrayOutputStream out = null;
				
				try {
					in = new FileInputStream(classFile);
					out = new ByteArrayOutputStream();
					byte [] buff = new byte[1024];
					int len;
					while ((len = in.read(buff)) != -1){
						out.write(buff,0,len);
					}
					return defineClass(className,out.toByteArray(),0,out.size());
					
				} catch (Exception e) {
					
				}finally {
					
				}
				
			}
		}
		
		return null;
	}

}
